diff --git a/plugin/Socket/footer.php b/plugin/Socket/footer.php index c22de3fc9..9da1b7ab6 100644 --- a/plugin/Socket/footer.php +++ b/plugin/Socket/footer.php @@ -1,231 +1,234 @@ debugAllUsersSocket) || (User::isAdmin() && !empty($obj->debugSocket))) { $socket_info_container_class = ''; $socket_info_container_top = 60; $socket_info_container_left = 50; if ($_COOKIE['socketInfoMinimized']) { $socket_info_container_class = 'socketMinimized'; } if ($_COOKIE['socketInfoPositionTop']) { $socket_info_container_top = $_COOKIE['socketInfoPositionTop']; } if ($_COOKIE['socketInfoPositionLeft']) { $socket_info_container_left = $_COOKIE['socketInfoPositionLeft']; } ?> diff --git a/plugin/Socket/functions.php b/plugin/Socket/functions.php index 45a9d45e2..8e5f3d3a5 100644 --- a/plugin/Socket/functions.php +++ b/plugin/Socket/functions.php @@ -1,107 +1,121 @@ from_users_id = User::getId(); $msgObj->isAdmin = User::isAdmin(); $msgObj->user_name = User::getNameIdentification(); $msgObj->browser = get_browser_name(); $msgObj->yptDeviceId = getDeviceID(false); $msgObj->token = getToken($timeOut); $msgObj->time = time(); - $msgObj->selfURI = getSelfURI(); $msgObj->ip = getRealIpAddr(); $msgObj->send_to_uri_pattern = $send_to_uri_pattern; $msgObj->autoEvalCodeOnHTML = array(); + + if (!empty($_REQUEST['webSocketSelfURI'])) { + $msgObj->selfURI = $_REQUEST['webSocketSelfURI']; + } else { + $msgObj->selfURI = getSelfURI(); + } + $msgObj->selfURI = getSelfURI(); if (empty($msgObj->videos_id)) { - $msgObj->videos_id = getVideos_id(); + if (!empty($_REQUEST['webSocketVideos_id'])) { + $msgObj->videos_id = $_REQUEST['webSocketVideos_id']; + } else { + $msgObj->videos_id = getVideos_id(); + } } if (empty($msgObj->live_key)) { - $msgObj->live_key = isLive(); + if (!empty($_REQUEST['webSocketLiveKey'])) { + $msgObj->live_key = json_decode($_REQUEST['webSocketLiveKey']); + } else { + $msgObj->live_key = isLive(); + } } if (AVideoPlugin::isEnabledByName('User_location')) { $msgObj->location = User_Location::getThisUserLocation(); } else { $msgObj->location = false; } /* if (!empty($msgObj->live_key)) { $msgObj->is_live = Live::isLiveAndIsReadyFromKey($msgObj->live_key['key'], $msgObj->live_key['live_servers_id'], true); if($msgObj->is_live){ $code = "onlineLabelOnline('.liveOnlineLabel');"; }else{ $code = "onlineLabelOffline('.liveOnlineLabel');"; } $msgObj->autoEvalCodeOnHTML[] = $code; } * */ return encryptString(json_encode($msgObj)); } function getDecryptedInfo($string) { $decriptedString = decryptString($string); $json = json_decode($decriptedString); if (!empty($json) && !empty($json->token)) { if (isTokenValid($json->token)) { return $json; } else { _error_log("socket:getDecryptedInfo: token is invalid "); } } else { _error_log("socket:getDecryptedInfo: json->token is empty ({$decriptedString})"); } return false; } class SocketMessageType { const NEW_CONNECTION = "NEW_CONNECTION"; const NEW_DISCONNECTION = "NEW_DISCONNECTION"; const DEFAULT_MESSAGE = "DEFAULT_MESSAGE"; const ON_VIDEO_MSG = "ON_VIDEO_MSG"; const ON_LIVE_MSG = "ON_LIVE_MSG"; } function getTotalViewsLive_key($live_key) { if (empty($live_key)) { return false; } $live_key = object_to_array($live_key); _mysql_connect(); $liveUsersEnabled = \AVideoPlugin::isEnabledByName("LiveUsers"); if ($liveUsersEnabled) { $liveUsers = new \LiveOnlineUsers(0); $total = $liveUsers->getTotalUsersFromTransmitionKey($live_key['key'], $live_key['live_servers_id']); } else { $total = null; } _mysql_close(); return $total; } function killProcessOnPort() { $obj = \AVideoPlugin::getDataObject("Socket"); $port = intval($obj->port); - if(!empty($port)){ - echo 'Searching for port: '.$port.PHP_EOL; + if (!empty($port)) { + echo 'Searching for port: ' . $port . PHP_EOL; $command = 'netstat -ano | findstr ' . $port; exec($command, $output, $retval); $pid = getPIDUsingPort($port); if (!empty($pid)) { - echo 'Killing, PID '.$pid.PHP_EOL; + echo 'Killing, PID ' . $pid . PHP_EOL; killProcess($pid); - }else{ - echo 'No Need to kill, port NOT found'.PHP_EOL; + } else { + echo 'No Need to kill, port NOT found' . PHP_EOL; } } -} \ No newline at end of file +} diff --git a/plugin/Socket/script.js b/plugin/Socket/script.js index 642773a70..9a4c65503 100644 --- a/plugin/Socket/script.js +++ b/plugin/Socket/script.js @@ -1,182 +1,186 @@ var socketConnectRequested = 0; var totalDevicesOnline = 0; function socketConnect() { if (socketConnectRequested) { return false; } socketConnectRequested = 1; var url = addGetParam(webSocketURL, 'page_title', $(document).find("title").text()); - console.log('Trying to reconnect on socket... ' + url); + console.log('Trying to reconnect on socket... '); conn = new WebSocket(url); conn.onopen = function (e) { console.log("Socket onopen"); return false; }; conn.onmessage = function (e) { var json = JSON.parse(e.data); parseSocketResponse(json); if (json.type == webSocketTypes.ON_VIDEO_MSG) { console.log("Socket onmessage ON_VIDEO_MSG", json); $('.videoUsersOnline, .videoUsersOnline_' + json.videos_id).text(json.total); } if (json.type == webSocketTypes.ON_LIVE_MSG) { console.log("Socket onmessage ON_LIVE_MSG", json); var selector = '#liveViewStatusID_' + json.live_key.key + '_' + json.live_key.live_servers_id; if (json.is_live) { onlineLabelOnline(selector); } else { onlineLabelOffline(selector); } } if (json.type == webSocketTypes.NEW_CONNECTION) { //console.log("Socket onmessage NEW_CONNECTION", json); } else if (json.type == webSocketTypes.NEW_DISCONNECTION) { //console.log("Socket onmessage NEW_DISCONNECTION", json); } else { var myfunc; if (json.callback) { console.log("Socket onmessage json.callback", json.callback); var code = "if(typeof " + json.callback + " == 'function'){myfunc = " + json.callback + ";}else{myfunc = defaultCallback;}"; //console.log(code); eval(code); } else { console.log("onmessage: callback not found", json); myfunc = defaultCallback; } myfunc(json.msg); } }; conn.onclose = function (e) { socketConnectRequested = 0; console.log('Socket is closed. Reconnect will be attempted in 1 second.', e.reason); setTimeout(function () { socketConnect(); }, 1000); }; conn.onerror = function (err) { socketConnectRequested = 0; console.error('Socket encountered error: ', err.message, 'Closing socket'); conn.close(); }; } function sendSocketMessageToAll(msg, callback) { sendSocketMessageToUser(msg, callback, ""); } function sendSocketMessageToNone(msg, callback) { sendSocketMessageToUser(msg, callback, -1); } function sendSocketMessageToUser(msg, callback, to_users_id) { if (conn.readyState === 1) { conn.send(JSON.stringify({msg: msg, webSocketToken: webSocketToken, callback: callback, to_users_id: to_users_id})); } else { console.log('Socket not ready send message in 1 second'); setTimeout(function () { sendSocketMessageToUser(msg, to_users_id, callback); }, 1000); } } function isSocketActive() { return typeof conn != 'undefined' && conn.readyState === 1; } function defaultCallback(json) { //console.log('defaultCallback', json); } function parseSocketResponse(json) { console.log("parseSocketResponse", json); if(json.isAdmin && webSocketServerVersion!==json.webSocketServerVersion){ avideoToastWarning("Please restart your socket server. You are running (v"+json.webSocketServerVersion+") and your client is expecting (v"+webSocketServerVersion+")"); } if (json && typeof json.autoUpdateOnHTML !== 'undefined') { $('.total_on').text(0); //console.log("parseSocketResponse", json.autoUpdateOnHTML); for (var prop in json.autoUpdateOnHTML) { if (json.autoUpdateOnHTML[prop] === false) { continue; } $('.' + prop).text(json.autoUpdateOnHTML[prop]); } } if (json && typeof json.autoEvalCodeOnHTML !== 'undefined') { for (var prop in json.autoEvalCodeOnHTML) { if (json.autoEvalCodeOnHTML[prop] === false) { continue; } //console.log("autoEvalCodeOnHTML", json.autoEvalCodeOnHTML[prop]); eval(json.autoEvalCodeOnHTML[prop]); } } $('#socketUsersURI').empty(); if (json && typeof json.users_uri !== 'undefined' && $('#socket_info_container').length) { for (var prop in json.users_uri) { if (json.users_uri[prop] === false) { continue; } for (var prop2 in json.users_uri[prop]) { if (json.users_uri[prop][prop2] === false || typeof json.users_uri[prop][prop2] !== 'object') { continue; } for (var prop3 in json.users_uri[prop][prop2]) { if (json.users_uri[prop][prop2][prop3] === false || typeof json.users_uri[prop][prop2][prop3] !== 'object') { continue; } var socketUserDivID = 'socketUser'+json.users_uri[prop][prop2][prop3].users_id; if(!$('#'+socketUserDivID).length){ var html = '
'; html += '
'; html += ' ' + json.users_uri[prop][prop2][prop3].user_name + '
'; html += '
'; $('#socketUsersURI').append(html); } var text = ''; if(json.ResourceID == json.users_uri[prop][prop2][prop3].resourceId){ text += '(YOU)' ; } text = ' ' + json.users_uri[prop][prop2][prop3].page_title; text += '
' + json.users_uri[prop][prop2][prop3].ip+'' ; if(json.users_uri[prop][prop2][prop3].location){ text += '
'; text += ' '+json.users_uri[prop][prop2][prop3].location.country_name; } html = ' '+text+''; $('#'+socketUserDivID+' .socketUserPages').append(html); var isVisible = Cookies.get('#'+socketUserDivID); if(isVisible && isVisible !== 'false'){ $('#'+socketUserDivID).addClass('visible') } } } } } } $(function () { console.log('Getting webSocketToken ...'); + var getWebSocket = webSiteRootURL + 'plugin/Socket/getWebSocket.json.php'; + getWebSocket = addGetParam(getWebSocket, 'webSocketSelfURI', webSocketSelfURI); + getWebSocket = addGetParam(getWebSocket, 'webSocketVideos_id', webSocketVideos_id); + getWebSocket = addGetParam(getWebSocket, 'webSocketLiveKey', webSocketLiveKey); $.ajax({ - url: webSiteRootURL + 'plugin/Socket/getWebSocket.json.php', + url: getWebSocket, success: function (response) { if(response.error){ console.log('Getting webSocketToken ERROR '+response.msg); avideoToastError(response.msg); }else{ console.log('Getting webSocketToken SUCCESS ', response); webSocketToken = response.webSocketToken; webSocketURL = response.webSocketURL; socketConnect(); } } }); }); \ No newline at end of file